﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using Entity.ProjectEnt;
using ZFine.Application.Project;
using ZFine.Application.SystemManage;
using ZFine.Code;
using ZFine.Web;

namespace Logiv.WEB.Areas.Project.Controllers
{
    public class CarriageController : BaseController
    {
        public CustomerAndThingsNameApp CTserverApp = new CustomerAndThingsNameApp();
        private CarriageApp serverApp = new CarriageApp();
      

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult indexss() {
            return View();
        }

        public ActionResult Form()
        {
            return View();
        }

        #region 接车查询
        public ActionResult JieCheIndex()
        {
            var reusltkehu = CTserverApp.GetList("1");//客户
            var reusltpingming = CTserverApp.GetList("0");//品名
            ViewBag.kehulist = reusltkehu;
            ViewBag.pingminglist = reusltpingming;
            return View();
        }

      
        /// <summary>
        /// 接车查询的方法
        /// </summary>
        /// <param name="pagination"></param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonArrive(Pagination pagination, string keyword,string StartTime,string EndTime,string carId,string customerName)
        {
            try
            {
                var reuslt = serverApp.GetListArrive(pagination, keyword, StartTime,EndTime,carId, customerName);
                var result = (from num in reuslt
                    select new CarriageEntity
                    {
                        F_Id = num.F_Id,
                        CarriageID = num.CarriageID,
                        CarType = num.CarType,
                        Capacity = num.Capacity,
                        Remark = num.Remark,
                        PathWay = num.PathWay,
                        ThingsName = num.ThingsName,
                        ThingsCount = num.ThingsCount,
                        Weight = num.Weight,
                        FenHao = num.FenHao,
                        ArriveTime = num.ArriveTime,
                        Customers = num.Customers

                    }).ToList();
                var data = new
                {
                    rows = result,
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records,
                   
                };
                return Content(data.ToJson());

            }
            catch (Exception ex)
            {
                return null;
            }

        }
        #endregion

        public ActionResult GetExportExcell(string keyword, string StartTime, string EndTime, string carId, string customerName)
        {
            Pagination pagination2 = new Pagination();
            pagination2.sord = "ArriveTime desc";
            pagination2.sidx = "ArriveTime";
            pagination2.page = 1;
            pagination2.rows = 1000000000;
            var reuslt = serverApp.GetListArrive(pagination2, keyword, StartTime, EndTime, carId, customerName);
            if (reuslt != null && reuslt.Count > 0)
            {
                List<ExcelObj> exportdata = new List<ExcelObj>();
                foreach (var Carriageobj in reuslt)
                {
                    ExcelObj temp = new ExcelObj(Carriageobj);
                    exportdata.Add(temp);
                }
                Type entityType = exportdata[0].GetType();
                PropertyInfo[] entityProperties = entityType.GetProperties();
                ArrayList list = new ArrayList();
                for (int i = 0; i < entityProperties.Length; i++)
                {
                    list.Add(entityProperties[i].Name);
                }
                string[] arrString = (string[])list.ToArray(typeof(string));
                ZFine.Code.AsposeExcel.SaveFile(ZFine.Code.AsposeExcel.ListToDataTable<ExcelObj>(exportdata), "接车数据.xls", arrString);
                return null;
            }
            return null;
        }
        /// <summary>
        /// 获取总车数 和总吨数
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="keyword"></param>
        /// <param name="StartTime"></param>
        /// <param name="EndTime"></param>
        /// <param name="carId"></param>
        /// <param name="customerName"></param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetTotalCarInfo(Pagination pagination, string keyword, string StartTime, string EndTime, string carId, string customerName)
        {
            try
            {
                Pagination pagination2=new Pagination();
                pagination2.sord = "ArriveTime desc";
                pagination2.sidx = "ArriveTime";
                pagination2.page = 1;
                pagination2.rows = 100000;
                var reuslt = serverApp.GetListArrive(pagination2, keyword, StartTime, EndTime, carId, customerName);

                int carNum = 0;
                carNum = reuslt.Count;
                decimal? totalWeight = 0;
                foreach (var item in reuslt)
                {
                    totalWeight += item.Weight;
                }
                var data = new 
                {
                    CarNum=carNum,
                    TotalWeight=totalWeight
                };

                return Content(data.ToJson());
            }
            catch(Exception ex)
            {
                return null;
            }
           
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJson(Pagination pagination)
        {
            try
            {
                var reuslt = serverApp.GetList();
                var result = (from num in reuslt
                    select new CarriageEntity
                    {
                        F_Id = num.F_Id,
                        CarriageID = num.CarriageID,
                        CarType = num.CarType,
                        Capacity = num.Capacity,
                        Remark = num.Remark,
                      
                    }).ToList();
                var data = new
                {
                    rows = result,
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records
                };
                return Content(data.ToJson());

            }
            catch (Exception ex)
            {
                return null;
            }

        }


        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateInput(false)]
        public ActionResult SubmitForm(CarriageEntity ServerEntity, string keyValue)
        {
            serverApp.SubmitForm(ServerEntity, keyValue);
            return Success("操作成功！");
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(string keyValue)
        {
            var data = serverApp.GetForm(keyValue);
            return Content(data.ToJson());
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [HandlerAuthorize]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteForm(string keyValue)
        {
            serverApp.DeleteForm(keyValue);
            return Success("删除成功");
        }
    }
    public class ExcelGudaoCarObj {
        private UserApp userApp = new UserApp();
        private PositionRecordApp posService = new PositionRecordApp();
        public string 日期 { get; set; }
        public string 班次 { get; set; }
        public string 调度员 { get; set; }
        public string 车型 { get; set; }
        public string 车号 { get; set; }
        public string 所属股道 { get; set; }
        public string 篷布号封号 { get; set; }
        public string 发货人 { get; set; }
        public string 到站 { get; set; }
        public string 品名 { get; set; }
        public string 件数 { get; set; }
        public string 重量 { get; set; }
        public string 备注 { get; set; }
        public ExcelGudaoCarObj(GudaoCarEntity gudaoCarEntity) {
            //this.日期= gudaoCarEntity.QiPiaoTime == null ? "暂无" : gudaoCarEntity.QiPiaoTime?.ToString("yyyy-MM-dd HH:mm:dd");
            this.日期 = gudaoCarEntity.QiPiaoTime == null ? "暂无" : gudaoCarEntity.QiPiaoTime?.ToString("yyyy-MM-dd");
            if (gudaoCarEntity.QiPiaoOper != null)
            {
                var Banci = posService.GetForm(f => f.F_Id == gudaoCarEntity.QiPiaoOper.Banci);
                if (Banci != null)
                {
                    this.班次 = Banci.BanciUser;
                }
                else {
                    this.班次 = "无";
                }
            }
            else {
                this.班次 = "无";
            }
            
            if (gudaoCarEntity.QiPiaoOper != null && !gudaoCarEntity.QiPiaoOper.F_CreatorUserId.IsEmpty()) {
                var OperationPersion = userApp.GetForm(gudaoCarEntity.QiPiaoOper.F_CreatorUserId);
                this.调度员 = OperationPersion.F_RealName;
            }
            else {
                this.调度员 = "暂无";
            }
            if (gudaoCarEntity.BelongCarriage != null)
            {
                this.车型 = gudaoCarEntity.BelongCarriage.CarType;
                this.车号 = gudaoCarEntity.BelongCarriage.CarriageID;
            }
            else {
                this.车型 = "暂无";
                this.车号 = "暂无";
            }
            if (gudaoCarEntity.BelongPathway != null)
            {
                this.所属股道 = gudaoCarEntity.BelongPathway.Name;
            }
            else {
                this.所属股道 = "暂无";
            }
            this.篷布号封号 = gudaoCarEntity.Fenhao;
            this.发货人 = gudaoCarEntity.Customers;
            this.到站 = gudaoCarEntity.ArriveStation;
            this.品名 = gudaoCarEntity.ThingsName;
            if (gudaoCarEntity.UnitNumber != null)
            {
                this.件数 = gudaoCarEntity.UnitNumber.ToString();
            }
            else {
                this.件数 = "0";
            }
           
            if (gudaoCarEntity.Weight != null)
            {
                this.重量 = gudaoCarEntity.Weight.ToString();
            }
            else {
                this.重量 = "0";
            }
               
            this.备注 = gudaoCarEntity.Remark;
        }
    }
    public class ExcelObj {
        public string 车号 { get; set; }
        public string 车型 { get; set; }
        public decimal 载重 { get; set; }
        public string 所属股道 { get; set; }
        public string 品名 { get; set; }
        public string 重量 { get; set; }
        public string 客户 { get; set; }

        public string 到达时间 { get; set; }
        public string 备注 { get; set; }

        public ExcelObj(CarriageEntity Carriage) {
            this.车号 = Carriage.CarriageID;
            this.车型 = Carriage.CarType;
            this.载重 = Carriage.Capacity;
            this.所属股道 = Carriage.PathWay;
            this.品名 = Carriage.ThingsName;
             
            if (Carriage.Weight != null) {
                this.重量 = Carriage.Weight.ToString();
            }
            else {
                this.重量 = "无";
            }
            this.客户 = Carriage.Customers;
            this.到达时间 = Convert.ToDateTime(Carriage.ArriveTime).ToString("yyyy-MM-dd HH:mm:dd");
            this.备注 = Carriage.Remark;
        }
    }


    public class YunLiangTongjiObj {

        private PositionRecordApp posService = new PositionRecordApp();
        UserApp useService = new UserApp();

        public string 统计时间 { get; set; }
        public string 班次 { get; set; }
        public string 每次运量吨数 { get; set; }
        public string 单次运量工资 { get; set; }
        public string 车号 { get; set; }
        public string 品名 { get; set; }
        public string 车型 { get; set; }
        public string 到站时间 { get; set; }
        public string 到站 { get; set; }
        public string 客户 { get; set; }
        public string 是否回退 { get; set; }
        public string 回退时间 { get; set; }
        public string 操作人员 { get; set; }
        public string 备注 { get; set; }

        public YunLiangTongjiObj(PersonalSalaryEntity personalSalaryEntity) {
            this.统计时间 = personalSalaryEntity.F_CreatorTime==null?"暂无": personalSalaryEntity.F_CreatorTime?.ToString("yyyy-MM-dd HH:mm:dd");
            this.班次 = posService.GetForm(f => f.F_Id == personalSalaryEntity.OperationGudaoCarEntity.Banci)?.BanciUser;
            this.每次运量吨数 = personalSalaryEntity.Weight.ToString();
            this.单次运量工资 = personalSalaryEntity.EveryTimeSalary.ToString();
            this.车号 = personalSalaryEntity.EveryCarID == null ? "" : personalSalaryEntity.EveryCarID.CarriageID;
            this.品名 = personalSalaryEntity.EveryCarID == null ? "" : personalSalaryEntity.EveryCarID.ThingsName;
            this.车型 = personalSalaryEntity.EveryCarID == null ? "" : personalSalaryEntity.EveryCarID.CarType;
            this.到站时间 = personalSalaryEntity.EveryCarID == null ? "" : personalSalaryEntity.EveryCarID.ArriveTime.ToString("yyyy-MM-dd HH:mm:dd");
            this.到站 = personalSalaryEntity.EveryCarID == null ? "" : personalSalaryEntity.EveryCarID.ArriveStation;
            this.客户 = personalSalaryEntity.EveryCarID == null ? "" : personalSalaryEntity.EveryCarID.Customers;
            if (personalSalaryEntity.OperationGudaoCarEntity != null)
            {
                this.是否回退 = personalSalaryEntity.OperationGudaoCarEntity.IsRollBack == true ? "是" : "否";
                this.回退时间 = personalSalaryEntity.OperationGudaoCarEntity.RollbackTime?.ToString("yyyy-MM-dd HH:mm:dd");
            }
            else 
            {
                this.是否回退 = "未知";
                this.回退时间 = "暂无";
            }
            this.操作人员 = useService.GetForm(personalSalaryEntity.OperationGudaoCarEntity?.F_CreatorUserId)?.F_RealName;
            this.备注 = personalSalaryEntity.Remake;
        }

    }
}